package com.example.imitation_webchat_demo.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

import com.example.imitation_webchat_demo.R;
import com.example.imitation_webchat_demo.adapter.BuddyAdapter;
import com.example.imitation_webchat_demo.util.AppUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

// 联系人列表fragment
public class ContactListFragment extends Fragment {
	@ViewInject(R.id.buddy_expandablelistview)
	private ExpandableListView expandableListview;

	private String[] group = new String[] { "在线好友", "我的好友", "我的同事" };
	private String[][] buddy = new String[][] { { "元芳", "雷小贱", "狄大人" },
			{ "高太后", "士兵甲", "士兵乙", "士兵丙" }, { "艺术家", "叫兽", "攻城师", "职业玩家" } };
	
	private String username;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_contact_list, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		ViewUtils.inject(this, getActivity());
		initComponent(); // 初始化控件
		addListener(); // 添加监听
	}

	// 初始化控件
	public void initComponent() {
		ExpandableListAdapter adapter = new BuddyAdapter(getActivity(),
				group, buddy);
		expandableListview.setAdapter(adapter);
		expandableListview.setGroupIndicator(null); // 去掉扩展列表的默认箭头
		
		username = AppUtils.getUsernameFromShare(getActivity());
	}

	// 添加监听
	public void addListener() {
		// 分组展开
		expandableListview
				.setOnGroupExpandListener(new OnGroupExpandListener() {
					public void onGroupExpand(int groupPosition) {

					}
				});
		// 分组关闭
		expandableListview
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {
					public void onGroupCollapse(int groupPosition) {

					}
				});

		// 子项单击
		expandableListview.setOnChildClickListener(new OnChildClickListener() {
			public boolean onChildClick(ExpandableListView arg0, View arg1,
					int groupPosition, int childPosition, long arg4) {
				Toast.makeText(
						getActivity(),
						group[groupPosition] + " : "
								+ buddy[groupPosition][childPosition],
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});
	}
}